package com.jy.datapipeline.importserver.support;

import com.alibaba.excel.util.ListUtils;
import com.jy.datapipeline.common.constants.ExpConstants;
import com.jy.datapipeline.common.exception.GlobalException;
import com.jy.datapipeline.common.util.StringUtil;
import com.jy.datapipeline.importserver.common.dto.RuleTemplateDto;
import com.jy.datapipeline.importserver.common.enums.impl.ReturnEnum;
import com.jy.datapipeline.importserver.entity.ImportRuleTable;
import com.mysql.cj.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @类名 ImportRuleSupport
 * @描述 TODO
 * @作者 白条君
 * @创建日期 2023/8/24 17:49
 * @版本 1.0
 */
public class ImportRuleSupport {

    /**
     * 集合中的字段列表转为字符串
     * @param importColumns
     * @return
     * @throws GlobalException
     */
    public static String listConvertString(List<ImportRuleTable.ImportColumn> importColumns) throws GlobalException {
        String columns = null;
        StringBuffer buffer = new StringBuffer();
        for (ImportRuleTable.ImportColumn importColumn : importColumns) {
            String column = importColumn.getColumn();
            String columnType = importColumn.getColumnType();
            String columnFormat = importColumn.getColumnFormat();

            if (!StringUtils.isNullOrEmpty(columnFormat)) {
                columns = StringUtil.concat(buffer, column, " ", columnType, " ", "\"", columnFormat, "\"", ",", "\n");
            } else {
                columns = StringUtil.concat(buffer, column, " ", columnType, ",", "\n");
            }
        }

        if (StringUtils.isNullOrEmpty(columns) || columns.length() == 0) {
            throw new GlobalException(ReturnEnum.SERVER_ERROR.getCode(), ExpConstants.GENERATOR_COLUMNS_FAIL);
        }

        //返回字符串时去掉最后一个逗号和换行符
        return columns.substring(0, columns.length() - 2);
    }

    /**
     * 根据字段名称生成字段对象集合
     * @param ruleTemplateDto
     * @param columns
     * @return
     */
    public static List<ImportRuleTable.ImportColumn> generateImportColumnList(RuleTemplateDto ruleTemplateDto, List<Map<String, Object>> columns) {
        List<ImportRuleTable.ImportColumn> importColumns = ListUtils.newArrayList();
        ImportRuleTable.ImportColumn importColumn;
        String[] columnNames = ruleTemplateDto.getTargetColumnList().split(",");
        for (String columnName : columnNames) {
            importColumn = new ImportRuleTable.ImportColumn();
            //去除开头的换行符
            columnName = columnName.replace("\n", "");
            //设置时间格式字段名称和格式
            if (columnName.contains(" ")) {
                String[] split = columnName.split("\\s", 2);
                String name = split[0];
                //去除字符串内的双引号和前后空格
                String format = split[1].replace("\"", "").trim();
                importColumn.setColumn(name);
                importColumn.setColumnFormat(format);
            }
            //设置非时间字段名称
            if (dm.jdbc.util.StringUtil.isEmpty(importColumn.getColumn())) {
                importColumn.setColumn(columnName);
            }
            //添加字段类型
            for (Map<String, Object> column : columns) {
                if (String.valueOf(column.get("column")).equals(importColumn.getColumn())) {
                    importColumn.setColumnType(String.valueOf(column.get("column_type")));
                    break;
                }
            }
            importColumns.add(importColumn);
        }
        return importColumns;
    }
}
